package com.resjz.milddog.Implement.menu;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.resjz.milddog.Interface.menu.MenuInterface;
import com.resjz.milddog.entity.Dept.DeptEntity;
import com.resjz.milddog.entity.menu.MenuEntity;
import com.resjz.milddog.entity.menu.RoleMenuEntity;
import com.resjz.milddog.service.menu.MenuService;
import com.resjz.milddog.service.menu.RoleMenuService;
import com.resjz.milddog.vo.dept.DeptVo;
import com.resjz.milddog.vo.menu.MenuVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@org.apache.dubbo.config.annotation.DubboService(version = "${dubbo.service.version}",retries = 2)
@Slf4j
public class MenuInterfaceImpl implements MenuInterface {
    @Autowired
    private RoleMenuService roleMenuService;
    @Autowired
    private MenuService menuService;

    @Override
    public Boolean saveMenu(MenuVo menuVo) {
        if(StringUtils.isEmpty(menuVo.getMenuId()))
            return menuService.save(BeanUtil.toBean(menuVo, MenuEntity.class));
        else
            return menuService.updateById(BeanUtil.toBean(menuVo, MenuEntity.class));
    }

    @Override
    public List<Long> getRoleMenu(Long roleId) {
        List<RoleMenuEntity> list = roleMenuService.list(new QueryWrapper<RoleMenuEntity>()
                .select("distinct menu_id").eq("role_id" ,roleId));
        return list.stream().map(RoleMenuEntity::getMenuId).collect(Collectors.toList());
    }

    @Override
    public List<MenuVo> getMenuListInArr(List<Long> arr) {
        List<MenuEntity> orilist = menuService.list(new QueryWrapper<MenuEntity>().in("menu_id",arr));
        List<MenuVo> backlist = new ArrayList<>();
        orilist.stream().forEach(ori->{
            backlist.add(BeanUtil.toBean(ori,MenuVo.class));
        });
        return backlist;
    }
    @Override
    public List<MenuVo> getDeptArr() {
        List<MenuVo> backlist=new ArrayList<>();
        List<MenuEntity> menulist = menuService.list();
        if(menulist.size()<=0) return backlist;

        Map<Long, List<MenuEntity>> collect = menulist.stream().collect(Collectors.groupingBy(MenuEntity::getParentId));
        List<MenuEntity> templist = collect.get(0L);
        for (MenuEntity menuEntity : templist) {
            MenuVo deptVo = new MenuVo();
            BeanUtil.copyProperties(menuEntity,deptVo);
            deptVo.setSupername("");
            List<MenuVo> sublist =  findchild(collect,menuEntity.getMenuId(),menuEntity.getTitle());
            if (sublist.size() > 0) {
                deptVo.setChildren(sublist);
            }
            backlist.add(deptVo);
        }
        return backlist;
    }
    private List<MenuVo> findchild(Map<Long, List<MenuEntity>> collect,Long pid ,String pname){
        List<MenuEntity> templist = collect.get(pid);
        List<MenuVo> dglist = new ArrayList<>();
        if(templist==null||templist.size()<=0){
            return dglist;
        }else{
            for (MenuEntity menuEntity : templist) {
                MenuVo deptVo = new MenuVo();
                BeanUtil.copyProperties(menuEntity,deptVo);
                deptVo.setSupername(pname);
                List<MenuVo> sublist =  findchild(collect,menuEntity.getMenuId(),menuEntity.getTitle());
                if (sublist.size() > 0) {
                    deptVo.setChildren(sublist);
                }
                dglist.add(deptVo);
            }
            return  dglist;
        }
    }
}
